<?php
namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Intervention\Image\ImageManagerStatic as Image;//缩放
class MemberController extends Controller
{
	public function show()
	{
		// return 111;
		return view('/home/myhouse/member/update');
	}

	public function update(Request $request)
    {

        $data = $request -> except('_token','_method','oldpic');
        // dd($data);
        $this->validate($request, [
            //验证规则，required(不为空)
            'nickname' => 'max:8',
            'email' => 'required|email',
            'phone' => 'required|numeric',
            // 'code' => 'required',
        ],[
             //错误提醒
             'nickname.max' => '昵称不能超过8位',
             'email.required' => '邮箱不能为空',
             'email.email' => '邮箱格式不正确',
             'phone.required' => '手机号码不能为空', 
             'phone.mumeric' => '手机号码格式不正确或不少于11位',             
        ]);
        $oldPic = '.'.$request -> input('oldPic');
        //处理图片
        if($request ->hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                // echo $suffix;

                //随机文件名称
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                
                $data['pic'] = '/uploads/member/'.$fileName;
                // dd($data);
                //将文件移入指定文件夹中
                $request -> file('pic') -> move('./uploads/member/',$fileName); 


                //等比缩放start
                $fname = './uploads/member/' . '/' . $fileName;//缩放
                $srcarr = getimagesize($fname);
                $w=200;
                $h=200;
                if($w/$srcarr[0] > $h/$srcarr[1]){
                    $bili = $h/$srcarr[1];
                }else{
                    $bili = $w/$srcarr[0];
                }
                $nw = floor($srcarr[0]*$bili);
                $nh = floor($srcarr[1]*$bili);
                $img = Image::make($fname)->resize($nw,$nh)->save($fname);//缩放
                //等比缩放end

                //更新的同时删除旧图片
                if($oldPic != '.')
                {
                    if(file_exists($oldPic))
                    {
                        unlink($oldPic);//删除旧图片
                    }
                }
               
            }

        }

        //更新数据
        $res = DB::table('member') -> where('member_id',$data['member_id']) -> update($data);    
        // dd($res);
        if($res)
        {
        	$arr = DB::table('member') ->where('member_id',$data['member_id'])->first();
            session(['pic'=>$arr->pic]);
            session(['nickname'=>$arr->nickname]);
            session(['phone' => $arr ->phone]);
            session(['email' => $arr ->email]);
            echo '<script>alert("更改成功");window.location.href="/";</script>';
        }else
        {
            return back() -> with(['error' => '更新失败']);
        }

    }
}
